package com.og.ouroa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.ouroa.common.exception.Assert;
import com.og.ouroa.common.result.ResponseEnum;
import com.og.ouroa.pojo.entity.StudentNode;
import com.og.ouroa.mapper.StudentNodeMapper;
import com.og.ouroa.service.StudentNodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Us
 * @since 2021-12-13
 */
@Service
public class StudentNodeServiceImpl extends ServiceImpl<StudentNodeMapper, StudentNode> implements StudentNodeService {

    @Override
    public void addNode(Integer sid, String title, String html) {

        StudentNode search = search(sid, title);

        Assert.isNull(search, ResponseEnum.REPEART_TITLE);

        StudentNode studentNode = new StudentNode();
        studentNode.setSid(sid);
        studentNode.setTitle(title);
        studentNode.setArticle(html);

        baseMapper.insert(studentNode);

    }

    @Override
    public void remove(Integer id) {

        baseMapper.deleteById(id);
    }



    @Override
    public StudentNode search( Integer sid,String title) {
        QueryWrapper<StudentNode> wrapper = new QueryWrapper<>();

        if(sid!=null)
            wrapper.eq("sid",sid);

        if (title!=null)
            wrapper.eq("title",title);

        return baseMapper.selectOne(wrapper);
    }

    @Override
    public IPage<StudentNode> fuzzySearch(Page<StudentNode> pages, String title) {

        QueryWrapper<StudentNode> wrapper = new QueryWrapper<>();

        wrapper.like("title",title);


        return baseMapper.selectPage(pages,wrapper);
    }

    @Override
    public IPage<StudentNode> listTop(Page<StudentNode> pages) {
        QueryWrapper<StudentNode> wrapper = new QueryWrapper<>();

        return baseMapper.selectPage(pages,wrapper);
    }

    @Override
    public IPage<StudentNode> listTopByid(Page<StudentNode> pages,Integer sid) {
        QueryWrapper<StudentNode> wrapper = new QueryWrapper<>();

        wrapper.like("sid",sid);

        return baseMapper.selectPage(pages,wrapper);

    }

    @Override
    public void updata(Integer sid, String title, String html) {
        StudentNode search = search(sid, title);

        Assert.notNull(search, ResponseEnum.FILE_NOT_FOUND);

        QueryWrapper<StudentNode> wrapper = new QueryWrapper<>();
            wrapper.eq("sid",sid);
            wrapper.eq("title",title);
        StudentNode studentNode = new StudentNode();
        studentNode.setSid(sid);
        studentNode.setTitle(title);
        studentNode.setArticle(html);

        baseMapper.update(studentNode,wrapper);
    }


}
